package com.stylefeng.guns.rest.modular.wz.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.enums.*;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.dao.WzFinanceDao;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import com.stylefeng.guns.rest.pay.ali.AlipayConfig;
import com.stylefeng.guns.rest.pay.wx.util.ConstantUtil;
import com.stylefeng.guns.rest.pay.wx.util.WXUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static com.stylefeng.guns.rest.pay.wx.WxPrepay.createSign;
import static com.stylefeng.guns.rest.pay.wx.WxPrepay.getTransfers;

/**
 * 资金处理
 */
@Service
public class WzFinanceService {

    @Autowired
    private WzUserFinanceRecordMapper userFinanceRecordMapper;
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private PfFinanceRecordMapper pfFinanceRecordMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzFinanceDao financeDao;
    @Autowired
    private WzUserMergeMapper userMergeMapper;
    @Autowired
    private WzUserMergeDetailMapper userMergeDetailMapper;
    @Autowired
    private WzCouponOrderMapper couponOrderMapper;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private WzMapMergeMapper mapMergeMapper;
    @Autowired
    private WzMapRebateMapper mapRebateMapper;
    @Autowired
    private WzUserRebateMapper userRebateMapper;
    @Autowired
    private WzCouponMarketMapper couponMarketMapper;

    /**
     * 优惠券扫码-用户入账
     */
    public boolean couponScan( WzUserCoupon userCoupon,WzCoupon coupon) {
        WzUser user = cacheService.user(coupon.getUserCode());//发布券的用户
        int price = userCoupon.getPrice();
        if(price == 0){
            price = coupon.getPrice();
        }
        //用户资金处理
        user.setRewardBalance(user.getRewardBalance() + price);//积分余额
        user.setProfit(user.getProfit() + price);//现金余额
        //资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());
        userFinanceRecord.setTradeNo(userCoupon.getUserCouponId().toString());
        userFinanceRecord.setAmount(price);//金额
        if(com.stylefeng.guns.rest.common.util.StringUtils.isBlank(userCoupon.getBenefit())){
            userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Coupon_Scan.getName());//标题
        }else{
            userFinanceRecord.setTitle(userCoupon.getBenefit() + "券收入");//标题
        }
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(user.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Coupon_Scan.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());

        WzUserFinanceRecord marketUserFinanceRecord = null;
        WzUser marketUser = null;
        if(userCoupon.getType() == EnWzCoupon.TYPE_Shopping.getIndex() && userCoupon.getMarketRebateProfit() > 0){//券库券
            WzMapRebate mapRebate = mapRebateMapper.selectById(Long.parseLong(userCoupon.getMapRebateId()));
            marketUser = cacheService.user(mapRebate.getMerchantUserCode());

            //用户资金处理
            marketUser.setRewardBalance(marketUser.getRewardBalance() + userCoupon.getMarketRebateProfit());//积分余额
            marketUser.setProfit(marketUser.getProfit() + userCoupon.getMarketRebateProfit());//现金余额
            //资金记录
            marketUserFinanceRecord = new WzUserFinanceRecord();
            marketUserFinanceRecord.setId(IdWorker.getId());
            marketUserFinanceRecord.setTargetId(marketUserFinanceRecord.getId().toString());
            marketUserFinanceRecord.setTradeNo(userCoupon.getUserCouponId().toString());
            marketUserFinanceRecord.setAmount(userCoupon.getMarketRebateProfit());//金额
            marketUserFinanceRecord.setTitle(userCoupon.getBenefit() + "券收入");//标题
            marketUserFinanceRecord.setPayTime(new Date());//支付时间
            marketUserFinanceRecord.setUserCode(marketUser.getUserCode());//用户编号
            marketUserFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
            marketUserFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
            marketUserFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Coupon_Scan.getIndex());//金额类型
            marketUserFinanceRecord.setCreateTime(new Date());//创建时间
            marketUserFinanceRecord.setCashBalance(marketUser.getCashBalance());
            marketUserFinanceRecord.setRewardBalance(marketUser.getRewardBalance());
            marketUserFinanceRecord.setProfit(marketUser.getProfit());
            marketUserFinanceRecord.setPayTime(new Date());
        }
        boolean r = financeDao.couponScan(user,userFinanceRecord,userCoupon,marketUserFinanceRecord,marketUser);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 活动发布-审核
     */
    public boolean addMapActivity(int expense,WzMapActivity mapActivity, WzCoupon coupon, WzUser user) {
        //用户资金处理
        user.setCashBalance(user.getCashBalance()-expense);//现金余额
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//目标id：用户活动id
        userFinanceRecord.setTradeNo(mapActivity.getMapActivityId().toString());
        userFinanceRecord.setAmount(expense);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map_Activity.getName() + "审核");//标题
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(mapActivity.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map_Activity.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.addMapActivity(mapActivity,coupon,userFinanceRecord,user);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 修改活动-审核
     */
    public boolean editMapActivity(int expense, WzMapActivity mapActivity, WzCoupon coupon, WzUser user) {
        //用户资金处理
        user.setCashBalance(user.getCashBalance()-expense);//现金余额
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//目标id：用户活动id
        userFinanceRecord.setTradeNo(mapActivity.getMapActivityId().toString());
        userFinanceRecord.setAmount(expense);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map_Activity.getName() + "审核");//标题
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(mapActivity.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map_Activity.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.editMapActivity(mapActivity,coupon,userFinanceRecord,user);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 自增券活动发布-审核驳回
     */
    public boolean mapActivityCheck(WzMapActivity mapActivity,WzUser user) {
        //用户资金
        List<WzUserFinanceRecord> userFinanceRecords = userFinanceRecordMapper.selectList(new EntityWrapper<WzUserFinanceRecord>().eq("trade_no",mapActivity.getMapActivityId().toString()).eq("type",EnWzUserFinanceRecord.TYPE_OUT.getIndex()).orderBy("pay_time",false));
        if(userFinanceRecords.size() > 0){
            int amount = userFinanceRecords.get(0).getAmount();
            //用户资金
            user.setCashBalance(user.getCashBalance() + amount);//现金余额
            //用户资金记录
            WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
            userFinanceRecord.setId(IdWorker.getId());
            userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//目标id：用户活动id
            userFinanceRecord.setTradeNo(mapActivity.getMapActivityId().toString());
            userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
            userFinanceRecord.setAmount(amount);//金额
            userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map_Activity.getName()+"驳回");//标题
            userFinanceRecord.setPayTime(new Date());//支付时间
            userFinanceRecord.setUserCode(user.getUserCode());//用户编号
            userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
            userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map_Activity.getIndex());//金额类型
            userFinanceRecord.setCreateTime(new Date());//创建时间
            userFinanceRecord.setCashBalance(user.getCashBalance());
            userFinanceRecord.setRewardBalance(user.getRewardBalance());
            userFinanceRecord.setProfit(user.getProfit());
            boolean r = financeDao.mapActivityCheck(user,userFinanceRecord,mapActivity);
            if(r){
                return true;
            }
        }
        return false;
    }

    /**
     * 购买礼券
     */
    public boolean buyCoupon(int totalAmount,WzCouponOrder couponOrder, WzUser user, int type) {
        //用户资金处理
        user.setCashBalance(user.getCashBalance() - totalAmount);
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(totalAmount);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Buy_Coupon.getName());//标题
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(user.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Buy_Coupon.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.buyCoupon(user,userFinanceRecord,couponOrder,type);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 广告领钱
     */
    public boolean advReward(WzAdv adv, WzUser user,int rewardAverage) {

        adv.setSurplusReward(adv.getSurplusReward() - rewardAverage);
        //用户资金处理
        user.setProfit(user.getProfit() + rewardAverage);
        user.setRewardBalance(user.getRewardBalance() + rewardAverage);
        //资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setTargetId(adv.getAdvId().toString());//唯一id号
        userFinanceRecord.setAmount(rewardAverage);
        userFinanceRecord.setCreateTime(new Date());
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_adv_reward.getName());
        userFinanceRecord.setRemark("广告id:"+adv.getAdvId());
        userFinanceRecord.setUserCode(user.getUserCode());
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_adv_reward.getIndex());
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //我的领钱记录
        WzUserAdvRecord advRecord = new WzUserAdvRecord();
        advRecord.setUserCode(user.getUserCode());
        advRecord.setAdvId(adv.getAdvId());
        advRecord.setCreateTime(new Date());
        advRecord.setCoverUrl(adv.getCoverUrl());
        advRecord.setNickName(user.getNickName());
        advRecord.setAvatarUrl(user.getAvatarUrl());
        advRecord.setReward(rewardAverage);
        boolean r = financeDao.advReward(adv,user,userFinanceRecord,advRecord);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 红包领钱
     */
    public boolean repackedReward(WzAdv adv, WzUser user,int rewardAverage) {
        //用户资金处理
        user.setProfit(user.getProfit() + rewardAverage);
        user.setRewardBalance(user.getRewardBalance() + rewardAverage);
        //广告资金处理
        adv.setSurplusReward(adv.getSurplusReward() - rewardAverage);
        //资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(rewardAverage);
        userFinanceRecord.setCreateTime(new Date());
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setPayMethod(EnWzUserFinanceRecord.payMethod_wz.getIndex());
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Repacked_reward.getName());
        userFinanceRecord.setRemark("广告id:" + adv.getAdvId());
        userFinanceRecord.setUserCode(user.getUserCode());
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Repacked_reward.getIndex());
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //我的领钱记录
        WzUserAdvRecord advRecord = new WzUserAdvRecord();
        advRecord.setUserCode(user.getUserCode());
        advRecord.setAdvId(adv.getAdvId());
        advRecord.setAdvMapId(adv.getAdvMapId().toString());
        advRecord.setCreateTime(new Date());
        advRecord.setCoverUrl(adv.getCoverUrl());
        advRecord.setNickName(user.getNickName());
        advRecord.setAvatarUrl(user.getAvatarUrl());
        advRecord.setReward(rewardAverage);
        advRecord.setSubtype(adv.getSubtype());
        advRecord.setTitle(adv.getTitle());
        boolean r = financeDao.repackedReward(adv,user,userFinanceRecord,advRecord);
        if(r){
            return true;
        }
        return false;
    }


    /**
     * 新增周边店铺
     */
    public boolean advMapAdd(WzAdvMap advMap, WzUser user,int amount) {
        //用户资金处理
        user.setCashBalance(user.getCashBalance() - amount);
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setTradeNo(advMap.getAdvMapId().toString());//内部交易号
        userFinanceRecord.setAmount(amount);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map.getName() + "审核");//标题
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(advMap.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map.getIndex());//金额类型
        userFinanceRecord.setRemark("周边店铺id:"+advMap.getAdvMapId());
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.advMapAdd(advMap,user,userFinanceRecord);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 周边店铺审核驳回
     */
    public boolean saveAdvMapCheckFail(WzUser user, WzAdvMap advMap,int amount) {
        //用户资金记录
        user.setCashBalance(user.getCashBalance() + amount);
        //用户资金
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setAmount(amount);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map.getName()+"驳回");//标题
        userFinanceRecord.setRemark("周边店铺id:"+advMap.getAdvMapId());
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(advMap.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.saveAdvMapCheckFail(advMap,user,userFinanceRecord);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 充值
     */
    public boolean recharge(String out_trade_no,String trade_no,WzUserFinanceRecord userFinanceRecord) {
        //用户数据
        List<WzUser> userList = userMapper.selectList(new EntityWrapper<WzUser>().eq("user_code",userFinanceRecord.getUserCode()));
        userList.get(0).setCashBalance(userList.get(0).getCashBalance() + userFinanceRecord.getAmount());
        //用户资金数据修改
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setTradeNo(trade_no);
        userFinanceRecord.setOutTradeNo(trade_no);
        userFinanceRecord.setCashBalance(userList.get(0).getCashBalance());
        userFinanceRecord.setRewardBalance(userList.get(0).getRewardBalance());
        userFinanceRecord.setProfit(userList.get(0).getProfit());
        userFinanceRecord.setRemark(userFinanceRecord.getTitle()+"成功");
        //平台资金
        PfFinanceRecord pfFinanceRecord = new PfFinanceRecord();
        pfFinanceRecord.setAmount(userFinanceRecord.getAmount());
        pfFinanceRecord.setCreateTime(new Date());
        pfFinanceRecord.setType(userFinanceRecord.getType());
        pfFinanceRecord.setStatus(userFinanceRecord.getStatus());
        pfFinanceRecord.setUserCode(userFinanceRecord.getUserCode());
        pfFinanceRecord.setTitle(userFinanceRecord.getTitle());
        pfFinanceRecord.setPayMethod(userFinanceRecord.getPayMethod());
        pfFinanceRecord.setPayType(userFinanceRecord.getPayType());
        pfFinanceRecord.setRemark(userFinanceRecord.getTitle()+"成功");
        pfFinanceRecord.setPayTime(new Date());
        boolean r = financeDao.recharge(userList.get(0),userFinanceRecord,pfFinanceRecord);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 周边店铺续费
     */
    public boolean renewalsAdvMap(WzAdvMap advMap, WzUser user,int expense) {
        //用户资金处理
        user.setCashBalance(user.getCashBalance() - expense);
        user.setHasAdvMap(1);
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setTradeNo(advMap.getAdvMapId().toString());//内部交易号
        userFinanceRecord.setAmount(expense);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map_Renewals.getName() + "成功");//标题
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(advMap.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map_Renewals.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.renewalsAdvMap(advMap, userFinanceRecord, user);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 发店铺红包
     */
    public boolean saveSetTopAdvMap(WzUser user, WzAdv adv,WzAdvMap advMap, List<WzUserAdv> userAdvs,int expense) {
        //用户资金处理
        user.setCashBalance(user.getCashBalance() - expense);
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setTradeNo(advMap.getAdvMapId().toString());//内部交易号
        userFinanceRecord.setAmount(expense);//金额
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Adv_Map_Repacket.getName() + "成功");//标题
        userFinanceRecord.setPayTime(new Date());//支付时间
        userFinanceRecord.setUserCode(advMap.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Adv_Map_Repacket.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setPayMethod(EnWzUserFinanceRecord.payMethod_wz.getIndex());//交易渠道:wz
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.saveSetTopAdvMap(userFinanceRecord, user, adv, advMap, userAdvs);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 拼单订单回调处理
     */
    public boolean mapMergeCallback(String trade_no, WzUserFinanceRecord userFinanceRecord,String buyerId) {
        //用户资金数据修改
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_hidden.getIndex());
        userFinanceRecord.setOutTradeNo(trade_no);
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getName()+"成功");
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setBuyerId(buyerId);
        //平台资金
        PfFinanceRecord pfFinanceRecord = new PfFinanceRecord();
        pfFinanceRecord.setAmount(userFinanceRecord.getAmount());
        pfFinanceRecord.setCreateTime(new Date());
        pfFinanceRecord.setType(userFinanceRecord.getType());
        pfFinanceRecord.setUserCode(userFinanceRecord.getUserCode());
        pfFinanceRecord.setTitle(userFinanceRecord.getTitle());
        pfFinanceRecord.setPayMethod(userFinanceRecord.getPayMethod());
        pfFinanceRecord.setPayType(userFinanceRecord.getPayType());
        pfFinanceRecord.setRemark(userFinanceRecord.getTitle());
        //拼单数据修改
        WzUserMergeDetail userMergeDetail = userMergeDetailMapper.selectById(Long.parseLong(userFinanceRecord.getTradeNo()));//拼单活动详情
        WzUserMerge userMerge = userMergeMapper.selectById(userMergeDetail.getUserMergeId());//拼单活动
        WzMapMerge mapMerge = mapMergeMapper.selectById(userMerge.getMapMergeId());
        if(userMergeDetail.getUserCode().toString().equals(userMerge.getUserCode().toString())){//拼单发起人
            userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_OnGoing.getIndex());
            Date date = DateUtil.getDateAfter(new Date(),Integer.parseInt(Consts.mergeEndDay));
            if(date.after(mapMerge.getEndTime())){
                date = mapMerge.getEndTime();
            }
            userMergeDetail.setEndTime(date);
            userMergeDetail.setBuyerId(userFinanceRecord.getBuyerId());
            userMergeDetail.setPayMethod(userFinanceRecord.getPayMethod());
            userMerge.setStatus(EnWzMapMerge.USERMERGE_STATUS_GOING.getIndex());
            userMerge.setEndTime(userMergeDetail.getEndTime());
            userMerge.setQuantity(userMerge.getQuantity() + userMergeDetail.getQuantity());
            boolean r = financeDao.mapMergeCallback(userFinanceRecord,pfFinanceRecord,userMergeDetail,userMerge);
            return r;
        }else{//拼单参与人
            userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Finish.getIndex());//拼单详情结束
            userMergeDetail.setBuyerId(userFinanceRecord.getBuyerId());
            userMergeDetail.setPayMethod(userFinanceRecord.getPayMethod());
            List<WzUserMergeDetail> createUserMergeDetails = userMergeDetailMapper.selectList(new EntityWrapper<WzUserMergeDetail>().eq("user_Code",userMerge.getUserCode()).eq("user_merge_id",userMerge.getUserMergeId()));
            userMerge.setStatus(EnWzMapMerge.USERMERGESTATUS_END.getIndex());//拼单结束
            userMerge.setJoinNumber(userMerge.getJoinNumber() + 1);
            userMerge.setQuantity(userMergeDetail.getQuantity());

            mapMerge.setSales(mapMerge.getSales() + userMergeDetail.getQuantity());
            mapMerge.setSurplusNumber(mapMerge.getSurplusNumber() - userMergeDetail.getQuantity());//拼单剩余数量
            if(mapMerge.getSurplusNumber() <= 0){
                mapMerge.setStatus(EnWzMapMerge.STATUS_END.getIndex());
            }
            WzUser user = cacheService.user(userMergeDetail.getUserCode());
            String avatarUrl=user.getAvatarUrl();
            if(!avatarUrl.startsWith("http")){
                avatarUrl=Consts.DOMAIN+avatarUrl;
            }

            //礼券生成
            WzAdvMap advMap = cacheService.advMap(mapMerge.getAdvMapId());
            WzCoupon coupon = cacheService.coupon(mapMerge.getCouponId());
            List<WzUserCoupon> userCoupons = new ArrayList<>();
            //自己券生成
            for(int i = 0;i < userMergeDetail.getQuantity();i++){
                WzUserCoupon userCoupon = new WzUserCoupon();
                userCoupon.setUserCouponId(IdWorker.getId());
                userCoupon.setCouponId(mapMerge.getCouponId());
                userCoupon.setMapMergeId(mapMerge.getMapMergeId().toString());
                userCoupon.setUserCode(user.getUserCode());
                userCoupon.setAdvMapId(mapMerge.getAdvMapId().toString());
                userCoupon.setNickName(user.getNickName());
                userCoupon.setAvatarUrl(user.getAvatarUrl());
                userCoupon.setType(EnWzCoupon.TYPE_Merge.getIndex());
                userCoupon.setName(coupon.getMerchantName());
                userCoupon.setLogo(coupon.getLogo());
                userCoupon.setBeginTime(coupon.getBeginTime());
                userCoupon.setEndTime(coupon.getEndTime());
                userCoupon.setBenefit(coupon.getBenefit());
                userCoupon.setCreateTime(new Date());
                userCoupon.setPrice(mapMerge.getMergePrice());
                userCoupon.setUserInfoId(userMergeDetail.getUserInfoId());
                userCoupon.setBuyerId(userMergeDetail.getBuyerId());
                userCoupon.setPayMethod(userMergeDetail.getPayMethod());
                userCoupons.add(userCoupon);
            }
            List<WzUserCoupon> createUserCoupons = new ArrayList<>();
            //别人的券生成
            WzUserMergeDetail createUserMergeDetail = createUserMergeDetails.get(0);
            if(createUserMergeDetail.getStatus() == EnWzMapMerge.UserMapMergeDetail_OnGoing.getIndex()){//没有处理过
                mapMerge.setSales(mapMerge.getSales() + createUserMergeDetail.getQuantity());
                mapMerge.setSurplusNumber(mapMerge.getSurplusNumber() - createUserMergeDetail.getQuantity());//拼单剩余数量
                if(mapMerge.getSurplusNumber() <= 0){
                    mapMerge.setStatus(EnWzMapMerge.STATUS_END.getIndex());
                }
                WzUser joinUser = cacheService.user(createUserMergeDetail.getUserCode());
                createUserMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Finish.getIndex());
                for(int i = 0;i < createUserMergeDetail.getQuantity();i++){
                    WzUserCoupon userCouponCreate = new WzUserCoupon();
                    userCouponCreate.setUserCouponId(IdWorker.getId());
                    userCouponCreate.setCouponId(mapMerge.getCouponId());
                    userCouponCreate.setMapMergeId(mapMerge.getMapMergeId().toString());
                    userCouponCreate.setUserCode(joinUser.getUserCode());
                    userCouponCreate.setAdvMapId(mapMerge.getAdvMapId().toString());
                    userCouponCreate.setNickName(joinUser.getNickName());
                    userCouponCreate.setAvatarUrl(joinUser.getAvatarUrl());
                    userCouponCreate.setType(EnWzCoupon.TYPE_Merge.getIndex());
                    userCouponCreate.setName(coupon.getMerchantName());
                    userCouponCreate.setLogo(coupon.getLogo());
                    userCouponCreate.setBeginTime(coupon.getBeginTime());
                    userCouponCreate.setEndTime(coupon.getEndTime());
                    userCouponCreate.setBenefit(coupon.getBenefit());
                    userCouponCreate.setCreateTime(new Date());
                    userCouponCreate.setPrice(mapMerge.getMergePrice());
                    userCouponCreate.setUserInfoId(createUserMergeDetail.getUserInfoId());
                    userCouponCreate.setBuyerId(createUserMergeDetail.getBuyerId());
                    userCouponCreate.setPayMethod(createUserMergeDetail.getPayMethod());
                    createUserCoupons.add(userCouponCreate);
                }
                //数据保存
                boolean r = financeDao.joinMapMergeCallback(userFinanceRecord,pfFinanceRecord,userMerge,userMergeDetail,createUserMergeDetail,userCoupons,createUserCoupons,mapMerge);
                if(r){
                    return true;
                }
            }else{
                //数据保存
                boolean r = financeDao.joinMapMergeCallback(userFinanceRecord,pfFinanceRecord,userMerge,userMergeDetail,createUserMergeDetail,userCoupons,null,mapMerge);
                if(r){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 原价现金购买
     */
    public boolean originalPriceCashBuy(ParamMap paramMap, WzUser user, WzUserFinanceRecord userFinanceRecord,WzCouponOrder couponOrder) {
        int payMethod = paramMap.getIntMisValid("payMethod");//支付类型：1-支付宝;2-微信;
        Long couponId = paramMap.getLongMisValid("couponId");//券id

        //用户现金资金记录
        if(userFinanceRecord != null){
            userFinanceRecord.setTargetId(couponOrder.getCouponOrderId().toString());//唯一id号
            userFinanceRecord.setTradeNo(couponId.toString());//内部交易号
            userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Coupon_Order_Cash.getName() + "待支付");//标题
            userFinanceRecord.setUserCode(user.getUserCode());//用户编号
            userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
            userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_waitCheck.getIndex());//状态:待支付
            userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Coupon_Order_Cash.getIndex());//金额类型
            userFinanceRecord.setCreateTime(new Date());//创建时间
            if(payMethod == EnWzUserFinanceRecord.payMethod_alipay.getIndex()){//支付宝支付
                userFinanceRecord.setPayMethod(EnWzUserFinanceRecord.payMethod_alipay.getIndex());
            }else if(payMethod == EnWzUserFinanceRecord.payMethod_weixin.getIndex()){//微信支付
                userFinanceRecord.setPayMethod(EnWzUserFinanceRecord.payMethod_weixin.getIndex());
            }
            userFinanceRecord.setCashBalance(user.getCashBalance());
            userFinanceRecord.setRewardBalance(user.getRewardBalance());
            userFinanceRecord.setProfit(user.getProfit());
        }

        //数据保存
        boolean r = financeDao.originalPriceBuy(userFinanceRecord,couponOrder);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 原价券回调处理
     */
    public boolean couponOrderCashCallback(String trade_no, WzUserFinanceRecord userFinanceRecord,String buyerId) {
        //用户资金数据修改
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_hidden.getIndex());
        userFinanceRecord.setOutTradeNo(trade_no);
        userFinanceRecord.setRemark(userFinanceRecord.getTitle()+"支付成功");
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Coupon_Order_Cash.getName()+"成功");
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setBuyerId(buyerId);
        //平台资金
        PfFinanceRecord pfFinanceRecord = new PfFinanceRecord();
        pfFinanceRecord.setAmount(userFinanceRecord.getAmount());
        pfFinanceRecord.setCreateTime(new Date());
        pfFinanceRecord.setType(userFinanceRecord.getType());
        pfFinanceRecord.setUserCode(userFinanceRecord.getUserCode());
        pfFinanceRecord.setTitle(userFinanceRecord.getTitle());
        pfFinanceRecord.setPayMethod(userFinanceRecord.getPayMethod());
        pfFinanceRecord.setPayType(userFinanceRecord.getPayType());
        pfFinanceRecord.setRemark(userFinanceRecord.getTitle());

        WzCoupon coupon = cacheService.coupon(Long.parseLong(userFinanceRecord.getTradeNo()));
        WzUser user = cacheService.user(userFinanceRecord.getUserCode());
        WzCouponOrder couponOrder = couponOrderMapper.selectById(Long.parseLong(userFinanceRecord.getTargetId()));
        couponOrder.setStatus(EnWzCoupon.COUPONORDER_STATUS_HasPay.getIndex());
        //券生成
        WzUserCoupon userCoupon = new WzUserCoupon();
        userCoupon.setCouponId(coupon.getCouponId());
        userCoupon.setUserCode(userFinanceRecord.getUserCode());
        userCoupon.setAdvMapId(coupon.getAdvMapId());
        userCoupon.setNickName(user.getNickName());
        userCoupon.setAvatarUrl(user.getAvatarUrl());
        userCoupon.setType(EnWzCoupon.TYPE_OriginalPrice.getIndex());
        userCoupon.setName(coupon.getMerchantName());
        userCoupon.setLogo(coupon.getLogo());
        userCoupon.setBeginTime(coupon.getBeginTime());
        userCoupon.setEndTime(coupon.getEndTime());
        userCoupon.setBenefit(coupon.getBenefit());
        userCoupon.setCreateTime(new Date());
        userCoupon.setPrice(coupon.getPrice());
        userCoupon.setUserInfoId(couponOrder.getUserInfoId());
        userCoupon.setPayMethod(userFinanceRecord.getPayMethod());
        userCoupon.setBuyerId(userFinanceRecord.getBuyerId());
        List<WzUserCoupon> userCoupons = new ArrayList<>();
        for(int i = 0;i < couponOrder.getQuantity();i++){
            userCoupons.add(userCoupon);
        }
        //数据保存
        boolean r = financeDao.couponOrderCashCallback(userFinanceRecord,pfFinanceRecord,userCoupons,couponOrder);
        if(r){
            return true;
        }
        return false;
    }

    //现金支付
    public boolean createMergeCash(WzUserMergeDetail userMergeDetail,int realAmount,int quantity, WzMapMerge mapMerge, WzUser user,WzUserFinanceRecord userFinanceRecord,WzUserMerge userMerge) {
        //用户拼单
        userMerge.setMapMergeId(mapMerge.getMapMergeId());//拼单活动id
        userMerge.setCouponId(mapMerge.getCouponId());//券id
        userMerge.setUserCode(user.getUserCode());//用户编号
        userMerge.setNickName(user.getNickName());//用户昵称
        userMerge.setAvatarUrl(user.getAvatarUrl());//用户头像
        userMerge.setStatus(EnWzMapMerge.UserMapMergeDetail_Status_Wait_Pay.getIndex());//状态：待支付
        userMerge.setCreateTime(new Date());
        userMerge.setJoinNumber(1);
        //用户拼单详情
        userMergeDetail.setAdvMapId(mapMerge.getAdvMapId());
        userMergeDetail.setAmount(realAmount);
        userMergeDetail.setCashAmount(realAmount);
        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
        userMergeDetail.setAvatarUrl(user.getAvatarUrl());
        userMergeDetail.setCreateTime(new Date());
        userMergeDetail.setNickName(user.getNickName());
        userMergeDetail.setQuantity(quantity);
        userMergeDetail.setUserCode(user.getUserCode());
        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
        userMergeDetail.setPayTime(DateUtil.addMinute(new Date(),30));//30分钟内支付
        userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Status_Wait_Pay.getIndex());//待支付
        userMergeDetail.setMerchantName(mapMerge.getMerchantName());
        userMergeDetail.setBenefit(mapMerge.getBenefit());
        userMergeDetail.setPic(mapMerge.getPic());
        userMergeDetail.setMapMergeId(userMerge.getMapMergeId());
        userMergeDetail.setMergePrice(mapMerge.getMergePrice());
        //资金支付记录
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(realAmount);//金额
        userFinanceRecord.setTradeNo(userMergeDetail.getUserMergeDetailId().toString());//内部交易号
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getName() + "待支付");//标题
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getIndex());//金额类型
        userFinanceRecord.setUserCode(user.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_waitCheck.getIndex());//状态:待支付
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //数据保存
        boolean r = financeDao.createMerge(userMerge,userFinanceRecord,userMergeDetail);
        if(r){
            return true;
        }
        return false;
    }

//    public boolean createMergeCashReward(int realAmount, int quantity, WzMapMerge mapMerge, WzUser user, WzUserFinanceRecord userFinanceRecord, WzUserFinanceRecord userRewardFinanceRecord,WzUserMerge userMerge,WzUserMergeDetail userMergeDetail) {
//        //用户拼单
//        userMerge.setMapMergeId(mapMerge.getMapMergeId());//拼单活动id
//        userMerge.setCouponId(mapMerge.getCouponId());//券id
//        userMerge.setUserCode(user.getUserCode());//用户编号
//        userMerge.setNickName(user.getNickName());//用户昵称
//        userMerge.setAvatarUrl(user.getAvatarUrl());//用户头像
//        userMerge.setCreateTime(new Date());
//        userMerge.setJoinNumber(1);
//        userMerge.setQuantity(quantity);
//        if(realAmount == 0){
//            userMerge.setStatus(EnWzMapMerge.USERMERGE_STATUS_GOING.getIndex());//状态：进行中
//            userMerge.setEndTime(DateUtil.getDateAfter(new Date(),Integer.parseInt(Consts.mergeEndDay)));
////            userMerge.setEndTime(DateUtil.addMinute(new Date(),2));
//        }else{
//            userMerge.setStatus(EnWzMapMerge.USERMERGE_STATUS_Wait_Pay.getIndex());//状态：待支付
//        }
//        //用户拼单详情
//        userMergeDetail.setUserMergeDetailId(IdWorker.getId());
//        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
//        userMergeDetail.setAvatarUrl(user.getAvatarUrl());
//        userMergeDetail.setCreateTime(new Date());
//        userMergeDetail.setNickName(user.getNickName());
//        userMergeDetail.setQuantity(quantity);
//        userMergeDetail.setUserCode(user.getUserCode());
//        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
//        userMergeDetail.setRewardAmount(userRewardFinanceRecord.getAmount());
//        if(realAmount == 0){
//            userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_OnGoing.getIndex());//进行中
//            userMergeDetail.setEndTime(userMerge.getEndTime());
//        }else{
//            userMergeDetail.setPayTime(DateUtil.addMinute(new Date(),3));//3分钟内支付
//            userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Status_Wait_Pay.getIndex());//待支付
//        }
//        userMergeDetail.setPic(mapMerge.getPic());
//        userMergeDetail.setBenefit(mapMerge.getBenefit());
//        userMergeDetail.setMerchantName(mapMerge.getMerchantName());
//        userMergeDetail.setMapMergeId(userMerge.getMapMergeId());
//        //资金支付记录
//        if(userFinanceRecord != null){
//            userMergeDetail.setCashAmount(userFinanceRecord.getAmount());
//            userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
//            userFinanceRecord.setTradeNo(userMergeDetail.getUserMergeDetailId().toString());//内部交易号
//            userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getName());//标题
//            userFinanceRecord.setUserCode(user.getUserCode());//用户编号
//            userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
//            userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
//            userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getIndex());//金额类型
//            userFinanceRecord.setCreateTime(new Date());//创建时间
//            userFinanceRecord.setCashBalance(user.getCashBalance());
//            userFinanceRecord.setRewardBalance(user.getRewardBalance());
//            userFinanceRecord.setProfit(user.getProfit());
//        }
//        //资金支付记录
//        userRewardFinanceRecord.setTargetId(userRewardFinanceRecord.getId().toString());//唯一id号
//        userRewardFinanceRecord.setTradeNo(userMergeDetail.getUserMergeDetailId().toString());//内部交易号
//        userRewardFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Reward.getName());//标题
//        userRewardFinanceRecord.setUserCode(user.getUserCode());//用户编号
//        userRewardFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
//        userRewardFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
//        userRewardFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Merge_Order_Reward.getIndex());//金额类型
//        userRewardFinanceRecord.setCreateTime(new Date());//创建时间
//        userRewardFinanceRecord.setCashBalance(user.getCashBalance());
//        userRewardFinanceRecord.setRewardBalance(user.getRewardBalance());
//        userRewardFinanceRecord.setProfit(user.getProfit());
//        //数据保存
//        boolean r = financeDao.createMergeCashReward(userMerge,userFinanceRecord,userMergeDetail,userRewardFinanceRecord,user);
//        if(r){
//            return true;
//        }
//        return false;
//    }

    public boolean joinMergeCash(WzUserMergeDetail userMergeDetail,int realAmount, int quantity, WzMapMerge mapMerge, WzUser user, WzUserFinanceRecord userFinanceRecord, WzUserMerge userMerge) {
        //用户拼单详情
        userMergeDetail.setAdvMapId(mapMerge.getAdvMapId());
        userMergeDetail.setAmount(realAmount);
        userMergeDetail.setCashAmount(realAmount);
        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
        userMergeDetail.setAvatarUrl(user.getAvatarUrl());
        userMergeDetail.setCreateTime(new Date());
        userMergeDetail.setNickName(user.getNickName());
        userMergeDetail.setQuantity(quantity);
        userMergeDetail.setUserCode(user.getUserCode());
        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
        userMergeDetail.setPayTime(DateUtil.addMinute(new Date(),30));//30分钟内支付
        userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Status_Wait_Pay.getIndex());//待支付
        userMergeDetail.setMerchantName(mapMerge.getMerchantName());
        userMergeDetail.setBenefit(mapMerge.getBenefit());
        userMergeDetail.setPic(mapMerge.getPic());
        userMergeDetail.setMapMergeId(userMerge.getMapMergeId());
        userMergeDetail.setMergePrice(mapMerge.getMergePrice());
        //资金支付记录
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(realAmount);//金额
        userFinanceRecord.setTradeNo(userMergeDetail.getUserMergeDetailId().toString());//内部交易号
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getName() + "待支付");//标题
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getIndex());//金额类型
        userFinanceRecord.setUserCode(user.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_waitCheck.getIndex());//状态:待支付
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //数据保存
        boolean r = financeDao.joinMergeCash(userFinanceRecord,userMergeDetail);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 现金支付创建返利活动
     */
    public boolean createUserRebate(int realAmount, int quantity, WzMapRebate mapRebate, WzUser user, WzUserFinanceRecord userFinanceRecord, WzUserRebate userRebate) {
        //用户拼单
        userRebate.setMapRebateId(mapRebate.getMapRebateId());//返利活动id
        userRebate.setCouponId(mapRebate.getCouponId());//券id
        userRebate.setUserCode(user.getUserCode());//用户编号
        userRebate.setNickName(user.getNickName());//用户昵称
        String avatarUrl=user.getAvatarUrl();
        if(!avatarUrl.startsWith("http")){
            avatarUrl=Consts.DOMAIN+avatarUrl;
        }
        userRebate.setAvatarUrl(avatarUrl);//用户头像
        userRebate.setStatus(EnWzMapRebate.USERREBATE_STATUS_Wait_Pay.getIndex());//状态：待支付
        userRebate.setCreateTime(new Date());
        userRebate.setMerchantName(mapRebate.getMerchantName());
        userRebate.setBenefit(mapRebate.getBenefit());
        userRebate.setPic(mapRebate.getPic());
        userRebate.setQuantity(quantity);
        userRebate.setNeedShareNumber(mapRebate.getNeedShareNumber());
        WzCoupon coupon = cacheService.coupon(mapRebate.getCouponId());
        userRebate.setPrice(coupon.getPrice());
        userRebate.setRebatePrice(mapRebate.getRebatePrice());
        userRebate.setPayTime(DateUtil.addMinute(new Date(),3));//3分钟内支付
        //资金支付记录
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(realAmount);//金额
        userFinanceRecord.setTradeNo(userRebate.getUserRebateId().toString());//内部交易号
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Rebate_Order_Cash.getName() + "待支付");//标题
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Rebate_Order_Cash.getIndex());//金额类型
        userFinanceRecord.setUserCode(user.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_waitCheck.getIndex());//状态:待支付
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //数据保存
        boolean r = financeDao.createUserRebate(userRebate,userFinanceRecord);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 创建返利活动支付回调处理
     */
    public boolean userRebateCallback(String trade_no, WzUserFinanceRecord userFinanceRecord,String buyerId) {
        //用户资金数据修改
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_hidden.getIndex());
        userFinanceRecord.setOutTradeNo(trade_no);
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Rebate_Order_Cash.getName()+"成功");
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setBuyerId(buyerId);
        //平台资金
        PfFinanceRecord pfFinanceRecord = new PfFinanceRecord();
        pfFinanceRecord.setAmount(userFinanceRecord.getAmount());
        pfFinanceRecord.setCreateTime(new Date());
        pfFinanceRecord.setType(userFinanceRecord.getType());
        pfFinanceRecord.setUserCode(userFinanceRecord.getUserCode());
        pfFinanceRecord.setTitle(userFinanceRecord.getTitle());
        pfFinanceRecord.setPayMethod(userFinanceRecord.getPayMethod());
        pfFinanceRecord.setPayType(userFinanceRecord.getPayType());
        pfFinanceRecord.setRemark(userFinanceRecord.getTitle());
        //返利数据修改
        WzUserRebate userRebate = userRebateMapper.selectById(Long.parseLong(userFinanceRecord.getTradeNo().toString()));//发起的返利活动
        WzMapRebate mapRebate = mapRebateMapper.selectById(userRebate.getMapRebateId());
        //参数修改
        userRebate.setStatus(EnWzMapRebate.USERREBATE_STATUS_GOING.getIndex());//返利活动开始进行
        userRebate.setEndTime(DateUtil.getDateAfter(new Date(),1));
        userRebate.setBuyerId(userFinanceRecord.getBuyerId());
        userRebate.setPayMethod(userFinanceRecord.getPayMethod());
        mapRebate.setSales(mapRebate.getSales() + userRebate.getQuantity());//返利活动销量增加
        //礼券生成
        WzCoupon coupon = cacheService.coupon(mapRebate.getCouponId());
        WzUser user = cacheService.user(userRebate.getUserCode());
        //券库处理
        WzCouponMarket couponMarket = null;
        if(mapRebate.getCouponMarketId() != null){
            couponMarket = couponMarketMapper.selectById(mapRebate.getCouponMarketId());
            couponMarket.setSurplusNumber(couponMarket.getSurplusNumber() - userRebate.getQuantity());
            if(couponMarket.getSurplusNumber() >= couponMarket.getTotalNumber()){
                couponMarket.setStatus(EnWzCouponMarket.STATUS_END.getIndex());
            }
        }

        List<WzUserCoupon> userCoupons = new ArrayList<>();
        for(int i = 0;i < userRebate.getQuantity();i++){
            //券生成
            WzUserCoupon userCoupon = new WzUserCoupon();
            userCoupon.setUserCouponId(IdWorker.getId());
            userCoupon.setCouponId(coupon.getCouponId());
            userCoupon.setMapRebateId(mapRebate.getMapRebateId().toString());
            userCoupon.setUserCode(user.getUserCode());
            userCoupon.setAdvMapId(mapRebate.getAdvMapId().toString());
            userCoupon.setNickName(user.getNickName());
            userCoupon.setAvatarUrl(user.getAvatarUrl());
            if(mapRebate.getCouponMarketId() != null){
                userCoupon.setType(EnWzCoupon.TYPE_Shopping.getIndex());
                userCoupon.setCouponMarketId(mapRebate.getCouponMarketId().toString());
            }else{
                userCoupon.setType(EnWzCoupon.TYPE_Rebate.getIndex());
            }

            userCoupon.setName(coupon.getMerchantName());
            userCoupon.setLogo(coupon.getLogo());
            userCoupon.setBeginTime(coupon.getBeginTime());
            userCoupon.setEndTime(coupon.getEndTime());
            userCoupon.setBenefit(coupon.getBenefit());
            userCoupon.setCreateTime(new Date());
            userCoupon.setPrice(mapRebate.getRebatePrice());
            userCoupon.setUserRebateId(userRebate.getUserRebateId().toString());
            userCoupon.setUserInfoId(userRebate.getUserInfoId());
            userCoupon.setBuyerId(userFinanceRecord.getBuyerId());
            userCoupon.setPayMethod(userFinanceRecord.getPayMethod());
            userCoupons.add(userCoupon);
        }
        //数据保存
        boolean r = financeDao.userRebateCallback(userFinanceRecord,pfFinanceRecord,userRebate,userCoupons,mapRebate,couponMarket);
        if(r){
            return true;
        }
        return false;

    }

    public boolean createRebate(int realAmount,WzUserRebate userRebate, WzUser user, WzUserFinanceRecord userFinanceRecord) {
        //资金支付记录
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(realAmount);//金额
        userFinanceRecord.setTradeNo(userRebate.getUserRebateId().toString());//内部交易号
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Rebate_Order_Cash.getName() + "待支付");//标题
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Rebate_Order_Cash.getIndex());//金额类型
        userFinanceRecord.setUserCode(user.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_waitCheck.getIndex());//状态:待支付
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //数据保存
        boolean r = financeDao.createRebate(userRebate,userFinanceRecord);
        if(r){
            return true;
        }
        return false;
    }

    /**
     * 返利活动完成
     */
    public boolean userRebateEnd(WzRebateShare rebateShare,WzUserRebate userRebate) {
        //计算返利金额
        int singleRebateAmount = userRebate.getPrice() - userRebate.getRebatePrice();//单个返利金额
        int rebateAmount = userRebate.getQuantity()*singleRebateAmount;//总返利金额
        userRebate.setRebateAmount(rebateAmount);
        //用户资金变更
        WzUser user = cacheService.user(userRebate.getUserCode());
        user.setRewardBalance(user.getRewardBalance() + rebateAmount);
        user.setProfit(user.getProfit() + rebateAmount);
        //用户资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setTargetId(userRebate.getUserRebateId().toString());//唯一id号
        userFinanceRecord.setAmount(rebateAmount);//金额
        userFinanceRecord.setTradeNo(userRebate.getMapRebateId().toString());//内部交易号
        userFinanceRecord.setTitle(userRebate.getBenefit() + EnWzUserFinanceRecord.TITLE_Rebate_Order_RebateAmount.getName());//标题
        userFinanceRecord.setUserCode(userRebate.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Rebate_Order_RebateAmount.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        //数据保存
        boolean r = financeDao.userRebateEnd(user,userFinanceRecord,rebateShare,userRebate);
        return r;
    }

    /**
     * 礼券退款
     */
    public boolean refund(WzUserCoupon userCoupon,int amount,WzCouponMarket couponMarket,WzMapMerge mapMerge,WzMapRebate mapRebate) throws Exception{
        userCoupon.setStatus(EnWzCoupon.STATUS_Refund.getIndex());
        WzUser user = cacheService.user(userCoupon.getUserCode());
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(amount);//金额
        userFinanceRecord.setTradeNo(userCoupon.getUserCouponId().toString());//内部交易号
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Coupon_Refundt.getName());//标题
        userFinanceRecord.setUserCode(userCoupon.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_waitCheck.getIndex());//状态:待交易
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Coupon_Refundt.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        userFinanceRecord.setBuyerId(userCoupon.getBuyerId());
        if(userCoupon.getPayMethod() == EnWzUserFinanceRecord.payMethod_alipay.getIndex()){
            //实际金额换算成小数
            double payAmount = new BigDecimal(amount).multiply(new BigDecimal(0.01)).setScale( 2, BigDecimal.ROUND_DOWN ).doubleValue();
            String app_id = AlipayConfig.app_id;
            String private_key = AlipayConfig.private_key;
            String ali_public_key = AlipayConfig.alipay_public_key;
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",app_id,private_key,"json","GBK",ali_public_key,"RSA2");
            AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
            request.setBizContent("{" +
                    "\"out_biz_no\":\""+userFinanceRecord.getTargetId()+"\"," +
                    "\"payee_type\":\"ALIPAY_USERID\"," +
                    "\"payee_account\":\""+userFinanceRecord.getBuyerId()+"\"," +
                    "\"amount\":\""+payAmount+"\"," +
                    "\"payer_show_name\":\"外赚礼券退款\"," +
                    "\"remark\":\"外赚礼券退款\"" +
                    "}");
            AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_hidden.getIndex());
                userFinanceRecord.setPayTime(new Date());
                //平台资金
                PfFinanceRecord pfFinanceRecord = new PfFinanceRecord();
                pfFinanceRecord.setUserCode(userFinanceRecord.getUserCode());
                pfFinanceRecord.setAmount(userFinanceRecord.getAmount());
                pfFinanceRecord.setTradeNo(userFinanceRecord.getTradeNo());
                pfFinanceRecord.setPayMethod(userFinanceRecord.getPayMethod());
                pfFinanceRecord.setType(userFinanceRecord.getType());
                pfFinanceRecord.setPayType(userFinanceRecord.getPayType());
                pfFinanceRecord.setCreateTime(new Date());
                pfFinanceRecord.setPayTime(new Date());
                pfFinanceRecord.setTitle(userFinanceRecord.getTitle());
                pfFinanceRecord.setRemark(userFinanceRecord.getRemark());
                boolean r = financeDao.refund(userFinanceRecord,pfFinanceRecord,userCoupon,couponMarket,mapMerge,mapRebate);
                return r;
            }
        } else if(userCoupon.getPayMethod() == EnWzUserFinanceRecord.payMethod_weixin.getIndex()){
            //实际金额换算成小数
            String key = ConstantUtil.APP_KEY;
            String mch_id = ConstantUtil.PARTNER;
            Integer payAmount = userFinanceRecord.getAmount();
            SortedMap<String, String> params =new TreeMap<String,String>();
            params.put("check_name", "NO_CHECK");
            params.put("nonce_str", WXUtil.getNonceStr());
            params.put("partner_trade_no", userFinanceRecord.getTargetId());
            params.put("amount", payAmount.toString());  //最好直接前端处理完 穿过来 整数  后期计算下单速度快 容易出现问题
            params.put("spbill_create_ip", "8.8.8.8");
            params.put("desc", "外赚礼券退款");
            params.put("key", key);
            params.put("mchid", mch_id);
            params.put("mch_appid", ConstantUtil.APP_ID);//微信小程序appid
            params.put("openid",userFinanceRecord.getBuyerId());
            params.put("sign", createSign(params));
            Map rr = getTransfers(params);
            if(rr.get("result_code").equals("SUCCESS")){
                userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_hidden.getIndex());
                userFinanceRecord.setPayTime(new Date());
                //平台资金
                PfFinanceRecord pfFinanceRecord = new PfFinanceRecord();
                pfFinanceRecord.setUserCode(userFinanceRecord.getUserCode());
                pfFinanceRecord.setAmount(userFinanceRecord.getAmount());
                pfFinanceRecord.setTradeNo(userFinanceRecord.getTradeNo());
                pfFinanceRecord.setPayMethod(userFinanceRecord.getPayMethod());
                pfFinanceRecord.setType(userFinanceRecord.getType());
                pfFinanceRecord.setPayType(userFinanceRecord.getPayType());
                pfFinanceRecord.setCreateTime(new Date());
                pfFinanceRecord.setPayTime(new Date());
                pfFinanceRecord.setTitle(userFinanceRecord.getTitle());
                pfFinanceRecord.setRemark(userFinanceRecord.getRemark());
                boolean r = financeDao.refund(userFinanceRecord,pfFinanceRecord,userCoupon,couponMarket,mapMerge,mapRebate);
                return r;
            }
        }
        return false;
    }

    /**
     * 退款到积分余额
     */
    public boolean refundProfit(WzUserCoupon userCoupon,int amount,WzCouponMarket couponMarket,WzMapMerge mapMerge,WzMapRebate mapRebate) {
        userCoupon.setStatus(EnWzCoupon.STATUS_Refund.getIndex());
        //用户资金处理
        WzUser user = cacheService.user(userCoupon.getUserCode());
        user.setProfit(user.getProfit() + userCoupon.getPrice());
        user.setRewardBalance(user.getRewardBalance() + userCoupon.getPrice());
        //资金记录
        WzUserFinanceRecord userFinanceRecord = new WzUserFinanceRecord();
        userFinanceRecord.setId(IdWorker.getId());
        userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
        userFinanceRecord.setAmount(amount);//金额
        userFinanceRecord.setTradeNo(userCoupon.getUserCouponId().toString());//内部交易号
        userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Coupon_Refundt.getName());//标题
        userFinanceRecord.setUserCode(userCoupon.getUserCode());//用户编号
        userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_IN.getIndex());//入账
        userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:待交易
        userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Coupon_Refundt.getIndex());//金额类型
        userFinanceRecord.setCreateTime(new Date());//创建时间
        userFinanceRecord.setPayTime(new Date());
        userFinanceRecord.setCashBalance(user.getCashBalance());
        userFinanceRecord.setRewardBalance(user.getRewardBalance());
        userFinanceRecord.setProfit(user.getProfit());
        boolean r = financeDao.refundProfit(userFinanceRecord,userCoupon,couponMarket,mapMerge,mapRebate);
        return r;
    }

    /**
     * 参加拼单
     */
//    public boolean joinMergeReward(int realAmount, int quantity, WzMapMerge mapMerge, WzUser user, WzUserFinanceRecord userFinanceRecord, WzUserFinanceRecord userRewardFinanceRecord, WzUserMerge userMerge,WzUserMergeDetail userMergeDetail) {
//        //用户拼单详情
//        userMergeDetail.setUserMergeDetailId(IdWorker.getId());
//        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
//        userMergeDetail.setAvatarUrl(user.getAvatarUrl());
//        userMergeDetail.setCreateTime(new Date());
//        userMergeDetail.setNickName(user.getNickName());
//        userMergeDetail.setQuantity(quantity);
//        userMergeDetail.setUserCode(user.getUserCode());
//        userMergeDetail.setUserMergeId(userMerge.getUserMergeId());
//        userMergeDetail.setPayTime(DateUtil.addMinute(new Date(),3));//3分钟内支付
//        userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Status_Wait_Pay.getIndex());//待支付
//        userMergeDetail.setPic(mapMerge.getPic());
//        userMergeDetail.setBenefit(mapMerge.getBenefit());
//        userMergeDetail.setMerchantName(mapMerge.getMerchantName());
//        userMergeDetail.setMapMergeId(userMerge.getMapMergeId());
//        //资金支付记录
//        if(userFinanceRecord != null){
//            userFinanceRecord.setTargetId(userFinanceRecord.getId().toString());//唯一id号
//            userFinanceRecord.setTradeNo(userMergeDetail.getUserMergeDetailId().toString());//内部交易号
//            userFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getName());//标题
//            userFinanceRecord.setUserCode(user.getUserCode());//用户编号
//            userFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
//            userFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
//            userFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Merge_Order_Cash.getIndex());//金额类型
//            userFinanceRecord.setCreateTime(new Date());//创建时间
//            userFinanceRecord.setCashBalance(user.getCashBalance());
//            userFinanceRecord.setRewardBalance(user.getRewardBalance());
//            userFinanceRecord.setProfit(user.getProfit());
//        }
//        //资金支付记录
//        userRewardFinanceRecord.setId(IdWorker.getId());
//        userRewardFinanceRecord.setTargetId(userRewardFinanceRecord.getId().toString());//唯一id号
//        userRewardFinanceRecord.setTradeNo(userMergeDetail.getUserMergeDetailId().toString());//内部交易号
//        userRewardFinanceRecord.setTitle(EnWzUserFinanceRecord.TITLE_Merge_Order_Reward.getName());//标题
//        userRewardFinanceRecord.setUserCode(user.getUserCode());//用户编号
//        userRewardFinanceRecord.setType(EnWzUserFinanceRecord.TYPE_OUT.getIndex());//出账
//        userRewardFinanceRecord.setStatus(EnWzUserFinanceRecord.STATUS_SUCCESS.getIndex());//状态:成功
//        userRewardFinanceRecord.setPayType(EnWzUserFinanceRecord.TITLE_Merge_Order_Reward.getIndex());//金额类型
//        userRewardFinanceRecord.setCreateTime(new Date());//创建时间
//        userRewardFinanceRecord.setCashBalance(user.getCashBalance());
//        userRewardFinanceRecord.setRewardBalance(user.getRewardBalance());
//        userRewardFinanceRecord.setProfit(user.getProfit());
//        if(userFinanceRecord == null){//完全用积分抵扣
//            mapMerge.setSales(mapMerge.getSales() + userMergeDetail.getQuantity());
//            userMerge.setStatus(EnWzMapMerge.USERMERGESTATUS_END.getIndex());//状态：已完成
//            userMerge.setJoinNumber(userMerge.getJoinNumber() + 1);
//            userMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Finish.getIndex());//已完成
//            WzAdvMap advMap = cacheService.advMap(mapMerge.getAdvMapId());
//            WzCoupon coupon = cacheService.coupon(mapMerge.getCouponId());
//            List<WzUserCoupon> userCoupons = new ArrayList<>();
//            //自己券生成
//            WzUserCoupon userCoupon = new WzUserCoupon();
//            userCoupon.setCouponId(mapMerge.getCouponId());
//            userCoupon.setMapMergeId(mapMerge.getMapMergeId().toString());
//            userCoupon.setUserCode(user.getUserCode());
//            userCoupon.setAdvMapId(mapMerge.getAdvMapId().toString());
//            userCoupon.setNickName(user.getNickName());
//            userCoupon.setAvatarUrl(user.getAvatarUrl());
//            userCoupon.setType(EnWzCoupon.TYPE_Merge.getIndex());
//            userCoupon.setName(advMap.getName());
//            userCoupon.setLogo(advMap.getLogo());
//            userCoupon.setBeginTime(coupon.getBeginTime());
//            userCoupon.setEndTime(coupon.getEndTime());
//            userCoupon.setBenefit(coupon.getBenefit());
//            userCoupon.setCreateTime(new Date());
//            for(int i = 0;i < userMergeDetail.getQuantity();i++){
//                userCoupons.add(userCoupon);
//            }
//            List<WzUserCoupon> createUserCoupons = new ArrayList<>();
//            //别人的券生成
//            List<WzUserMergeDetail> userMergeDetails = userMergeDetailMapper.selectList(new EntityWrapper<WzUserMergeDetail>().eq("user_code",userMerge.getUserCode()).eq("user_merge_id",userMerge.getUserMergeId()));
//            WzUserMergeDetail createUserMergeDetail = userMergeDetails.get(0);
//            if(createUserMergeDetail.getStatus() == EnWzMapMerge.UserMapMergeDetail_OnGoing.getIndex()){
//                mapMerge.setSales(mapMerge.getSales() + createUserMergeDetail.getQuantity());
//                createUserMergeDetail.setStatus(EnWzMapMerge.UserMapMergeDetail_Finish.getIndex());
//                WzUser joinUser = cacheService.user(createUserMergeDetail.getUserCode());
//                WzUserCoupon userCouponCreate = new WzUserCoupon();
//                userCouponCreate.setCouponId(mapMerge.getCouponId());
//                userCouponCreate.setMapMergeId(mapMerge.getMapMergeId().toString());
//                userCouponCreate.setUserCode(joinUser.getUserCode());
//                userCouponCreate.setAdvMapId(mapMerge.getAdvMapId().toString());
//                userCouponCreate.setNickName(joinUser.getNickName());
//                userCouponCreate.setAvatarUrl(joinUser.getAvatarUrl());
//                userCouponCreate.setType(EnWzCoupon.TYPE_Merge.getIndex());
//                userCouponCreate.setName(advMap.getName());
//                userCouponCreate.setLogo(advMap.getLogo());
//                userCouponCreate.setBeginTime(coupon.getBeginTime());
//                userCouponCreate.setEndTime(coupon.getEndTime());
//                userCouponCreate.setBenefit(coupon.getBenefit());
//                userCouponCreate.setCreateTime(new Date());
//                for(int i = 0;i < createUserMergeDetail.getQuantity();i++){
//                    createUserCoupons.add(userCouponCreate);
//                }
//                //数据保存
//                boolean r = financeDao.joinMergeCashRewardAllCoupon(userMerge,userMergeDetail,createUserMergeDetail,userRewardFinanceRecord,user,userCoupons,createUserCoupons,mapMerge);
//                if(r){
//                    return true;
//                }
//            }else{
//                //数据保存
//                boolean r = financeDao.joinMergeCashRewardMyCoupon(userMerge,userMergeDetail,userRewardFinanceRecord,user,userCoupons,mapMerge);
//                if(r){
//                    return true;
//                }
//            }
//        }else{
//            //数据保存
//            boolean r = financeDao.joinMergeCashReward(userMerge,userFinanceRecord,userMergeDetail,userRewardFinanceRecord,user);
//            if(r){
//                return true;
//            }
//        }
//        return false;
//    }
}
